﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IRC.IRepairClouds.DataLayer;



namespace IRC.IRepairClouds.WebClient
{
    public static class Auth
    {
        public enum AuthType
        {
            InsufficientPermissions,
            InsufficientDataPermissions,
            InsufficientFilePermissions,
            ItemNotExists,
            SecurityException
        }

        public static void Invoke(AuthType authType)
        {
            switch (authType)
            {
                case AuthType.InsufficientPermissions:
                    InsufficientPermissions();
                    break;
                case AuthType.InsufficientDataPermissions:
                    InsufficientDataPermissions();
                    break;
                case AuthType.InsufficientFilePermissions:
                    InsufficientFilePermissions();
                    break;
                case AuthType.ItemNotExists:
                    ItemNotExists();
                    break;
                case AuthType.SecurityException:
                    throw new System.Security.SecurityException();
            }
        }

        public static void InsufficientPermissions()
        {
            HttpContext.Current.Server.Transfer(VirtualPathUtility.ToAbsolute("~/ErrorPages/InsufficientPermissions.aspx"));
        }

        public static void InsufficientDataPermissions()
        {
            HttpContext.Current.Server.Transfer(VirtualPathUtility.ToAbsolute("~/ErrorPages/InsufficientDataPermissions.aspx"), true);
        }

        public static void InsufficientFilePermissions()
        {
            HttpContext.Current.Server.Transfer(VirtualPathUtility.ToAbsolute("~/ErrorPages/InsufficientFilePermissions.aspx"), true);
        }

        public static void ItemNotExists()
        {
            HttpContext.Current.Server.Transfer(VirtualPathUtility.ToAbsolute("~/ErrorPages/ItemNotExists.aspx"), true);
        }

        public static bool Check(AuthRequirements required)
        {
            return Check(required, (int?)null);
        }

        public static bool Check(AuthRequirements required, int? localProgramId)
        {
            if (!EUser.IsLoggedIn)
                return false;
            else
                return true;

            if (required == null)
                return true;
            var ud = IRC.IRepairClouds.DataLayer.Cache.CacheManager.Users.Current;
            //var pd = ud.Partner.Cached;

            
            bool valid = true;
            foreach (var r in required.Roles)
            {

                if (r.Value == null || ud.Role.IsMatch(r.Value.Value))
                {
                    valid = true;
                    break;
                }
                
            }
            return valid;
        }

        public static bool Check(AuthRequirements required, AuthItemPartners authPartners)
        {
            return Check(required, authPartners, null);
        }

        public static bool Check(AuthRequirements required, AuthItemPartners authPartners, AuthItemPartner authPartner)
        {
            if (!EUser.IsLoggedIn)
                return false;
            if (required == null)
                return true;

            var ud = IRC.IRepairClouds.DataLayer.Cache.CacheManager.Users.Current;

            bool valid = false;

            if (authPartner != null)
            {

                if (authPartner.UserRole != null)
                {
                    if (ud.Role == authPartner.UserRole)
                        valid = true;
                }
                else
                    valid = true;
                
            }

            return valid;
        }

        public static void Check(AuthRequirements required, int? localProgramId, AuthType authType)
        {
            if (!Check(required, localProgramId))
                Invoke(authType);
        }

        public static void Check(AuthRequirements required, AuthItemPartners authPartners, AuthType authType)
        {
            Check(required, authPartners, null, authType);
        }

        public static void Check(AuthRequirements required, AuthItemPartners authPartners, AuthItemPartner authPartner, AuthType authType)
        {
            if (!Check(required, authPartners, authPartner))
                Invoke(authType);
        }


        public static bool Check(PagesAuthorization.Pages page)
        {
            return Check(page, (int?)null);
        }

        public static void Check(PagesAuthorization.Pages page, AuthType authType)
        {
            Check(page, (int?)null, authType);
        }

        public static bool Check(PagesAuthorization.Pages page, int? localProgramId)
        {
            if (!EUser.IsLoggedIn)
                return false;
            var a = PagesAuthorization.Authorized.Pages.GetValueDef(page, null);
            if (a == null)
                return false;
            if (localProgramId == null)
                return a.Item1;
            return Check(a.Item2, localProgramId.Value);
        }

        public static void Check(PagesAuthorization.Pages page, int? localProgramId, AuthType authType)
        {
            if (!Check(page, localProgramId))
                Invoke(authType);
        }

        public static bool Check(PagesAuthorization.Pages page, AuthItemPartners authPartners)
        {
            return Check(page, authPartners, null);
        }

        public static bool Check(PagesAuthorization.Pages page, AuthItemPartners authPartners, AuthItemPartner authPartner)
        {
            if (!EUser.IsLoggedIn)
                return false;
            var a = PagesAuthorization.Authorized.Pages.GetValueDef(page, null);
            if (a == null)
                return false;
            return Check(a.Item2, authPartners, authPartner);
        }

        public static void Check(PagesAuthorization.Pages page, AuthItemPartners authPartners, AuthType authType)
        {
            Check(page, authPartners, null, authType);
        }

        public static void Check(PagesAuthorization.Pages page, AuthItemPartners authPartners, AuthItemPartner authPartner, AuthType authType)
        {
            if (!Check(page, authPartners, authPartner))
                Invoke(authType);
        }
    }
}